﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ServeNHL.Models
{
    [MetadataType(typeof(VCLMembersMetadata))]
    public partial class VCL_MEMBERS
    {

        public int saveVCLMembers(VCLViewModel vcl)
        {
            int vclid = 0;
            foreach (var memeber in vcl.VCLMembers)
            {
                if (memeber.VCL_MEMBER_ID != 0)
                {
                    updateVCLMembers(memeber);
                }
                else
                {
                    insertVCLMembers(memeber);
                }
            }

            //vclid = vcl.VCLMembers.VCL_MEMBER_ID;
            return 1;
        }// end of public int saveVCLMembers(VCLViewModel vcl)

        public int insertVCLMembers(VCL_MEMBERS vclMem)
        {
            int vclid = 0;

            using(var dbconn = new fclick_dbEntities())
            {
                var temp = new VCL_MEMBERS {
                    VCL_ID = vclMem.VCL_ID,
                    USER_ID = vclMem.USER_ID,
                    STATUS = vclMem.STATUS,
                    CREATED_DATE = Helpers.createDate.getCreateDate(),
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate(),


                };

                dbconn.VCL_MEMBERS.Add(temp);
                dbconn.SaveChanges();

                vclid = temp.VCL_ID;
            }// end of using (var dbconn = new fclick_dbEntities())

            return vclid;
        }// end of public int insertVCLMembers(VCL_MEMBERS vcl)

        public int updateVCLMembers(VCL_MEMBERS vclMem)
        {
            int vclid = 0;

            using (var dbconn = new fclick_dbEntities())
            {

                var temp = new VCL_MEMBERS();

                if (!String.IsNullOrEmpty(vclMem.VCL_ID.ToString()) || vclMem.VCL_MEMBER_ID > 0)
                {
                    temp.VCL_ID = vclMem.VCL_ID;
                }
                if (!String.IsNullOrEmpty(vclMem.USER_ID.ToString()) || vclMem.USER_ID > 0)
                {
                    temp.USER_ID = vclMem.USER_ID;
                }
                if (!String.IsNullOrEmpty(vclMem.STATUS))
                {
                    temp.STATUS = vclMem.STATUS;
                }

                dbconn.VCL_MEMBERS.Add(temp);
                dbconn.SaveChanges();

                vclid = temp.VCL_MEMBER_ID;
            }// end of using (var dbconn = new fclick_dbEntities())
            return vclid;
        }// end of public int updateVCLMembers(VCL_MEMBERS vcl)

    }// end of public partial class VCLMembers
}